package tech.xs.api.domain.enums;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;
import lombok.ToString;
import tech.xs.common.framework.domain.enums.BaseEnum;

import java.util.*;

@Getter
@ToString
public enum FileScenario implements BaseEnum {
    SYS_CURR_USER_AVATAR(1, "/sys/user/curr/avatar/", false, new String[]{"jpg", "png"});


    @EnumValue
    @JsonValue
    private final int value;
    private final String path;
    /**
     * 文件扩展名
     */
    private final Set extension = new HashSet();
    /**
     * 是否是文件
     */
    private final boolean isFile;

    private static final Map<Integer, FileScenario> mappings = new HashMap<>(values().length);

    static {
        for (FileScenario item : values()) {
            mappings.put(item.getValue(), item);
        }
    }

    FileScenario(int value, String path, boolean isFile, String[] extension) {
        this.value = value;
        this.path = path;
        this.isFile = isFile;
        if (extension != null) {
            for (String item : extension) {
                if (StrUtil.isNotBlank(item)) {
                    this.extension.add(item.toLowerCase());
                }
            }
        }
    }

    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static FileScenario create(int value) {
        return mappings.get(value);
    }

    @Override
    public Integer getValue() {
        return value;
    }

}
